﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace BackupFolder
{
    public partial class Form1 : Form
    {
        string _LogName = "LogInfo.txt";
        string _logFileName = string.Empty;
        string _sourcePath = ConfigurationManager.AppSettings["SourcePath"].ToString();
        string _targetPath = ConfigurationManager.AppSettings["TargetPath"].ToString();
        string _backupTime = ConfigurationManager.AppSettings["TimeBackup"].ToString();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime dt = new DateTime(2013, 1, 2, 8, 9, 7);
                txtSourcePath.Text = dt.ToString("yyyyMMdd HH:mm");
                //BackupFolders();
                //string[] time = _backupTime.Split('|');
                //foreach (string t in time)
                //{
                //    string hhmm = DateTime.Now.ToString("HH:mm");
                //    if (t == hhmm)
                //        BackupFolders();
                //}
            }
            catch (Exception ex)
            {
                logErr2("Lỗi local: " + ex.Message);
            }            
        }

        private void BackupFolders()
        {
            try
            {
                string sourcePath = txtSourcePath.Text; // _sourcePath;
                string targetPath = txtTargetPath.Text;//_targetPath;

                string todayFolderName = DateTime.Today.ToString("yyyyMMdd");
                targetPath = System.IO.Path.Combine(targetPath, todayFolderName);

                if (!System.IO.Directory.Exists(targetPath))
                {
                    System.IO.Directory.CreateDirectory(targetPath);
                }
                _logFileName = targetPath + "\\" + _LogName;
                logErr(string.Format("********** START BACKUP {0} *************", DateTime.Now.ToString("ss:hh:mm dd/MM/yyyy")));

                if (System.IO.Directory.Exists(sourcePath))
                {
                    CopyToDirectory(sourcePath, targetPath);
                }

                logErr(string.Format("*** END BACKUP {0} ***", DateTime.Now.ToString("ss:hh:mm dd/MM/yyyy")));
            }
            catch (Exception ex)
            {
                logErr2("Lỗi local: " + ex.Message);
            }
        }

        private void CopyToDirectory(string sourcePath, string targetPath)
        {
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }
            //Copy all file
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                // Copy the files and overwrite destination files if they already exist. 
                foreach (string s in files)
                {
                    try
                    {
                        string fileName = System.IO.Path.GetFileName(s);
                        string destFile = System.IO.Path.Combine(targetPath, fileName);
                        System.IO.File.Copy(s, destFile, true);
                    }
                    catch (Exception ex)
                    {
                        logErr(" - Error: " + ex);
                    }
                    
                }               
            }
            //Copy all folder
            string[] folders = System.IO.Directory.GetDirectories(sourcePath);
            for (int i = 0; i < folders.Length; i++)
            {
                string folder = folders[i].Replace(sourcePath, "");
                string targetSubPath = System.IO.Path.Combine(targetPath, folder.TrimStart('\\'));
                CopyToDirectory(folders[i], targetSubPath);
            }
        }

        public void logErr(string strLog)
        {
            string path = _logFileName;
            if (!File.Exists(path))
            {
                File.Create(path).Dispose();
                using (StreamWriter tw = new StreamWriter(path))
                {
                    tw.WriteLine(strLog);
                    tw.Close();
                    tw.Dispose();
                }

            }
            else if (File.Exists(path))
            {
                using (StreamWriter tw = File.AppendText(path))
                {
                    tw.WriteLine(strLog);
                    tw.Close();
                    tw.Dispose();
                }
            }           
        }

        //Lỗi ghi tại máy chủ
        public void logErr2(string strLog)
        {
            string path = Environment.CurrentDirectory;
            path = path + "\\LogError.txt";
            if (!File.Exists(path))
            {
                File.Create(path).Dispose();
                using (StreamWriter tw = new StreamWriter(path))
                {
                    tw.WriteLine(strLog);
                    tw.Close();
                    tw.Dispose();
                }

            }
            else if (File.Exists(path))
            {
                using (StreamWriter tw = File.AppendText(path))
                {
                    tw.WriteLine(strLog);
                    tw.Close();
                    tw.Dispose();
                }
            }
        }


    }
}
